package zhaolu.com.example.Delicacy.MySQL;

import java.util.ArrayList;
import java.util.List;

import zhaolu.com.example.Delicacy.Entity.Dish;
import zhaolu.com.example.Delicacy.Entity.HistoryBean;
import zhaolu.com.example.Delicacy.Entity.ItemBean;

public class DishDAO extends MySQLHelper {
    public List<Dish> getDishList() {
        List<Dish> list = new ArrayList<>();
        try {
            getConnection();
            String sql = "select * from Dish";
            callableStatement = connection.prepareCall(sql);
            resultSet = callableStatement.executeQuery();
            while (resultSet.next()) {
                Dish item = new Dish();
                item.setIdFood(resultSet.getInt("idFood"));
                item.setName(resultSet.getString("name"));
                item.setPrice(resultSet.getInt("price"));
                item.setNote(resultSet.getString("note"));
                list.add(item);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            closeAll();
        }
        return list;
    }

    public List<ItemBean> getDishBeanList() {
        List<ItemBean> list = new ArrayList<>();
        try {
            getConnection();
            String sql = "select * from Dish";
            callableStatement = connection.prepareCall(sql);
            resultSet = callableStatement.executeQuery();
            while (resultSet.next()) {
                ItemBean item = new ItemBean();
                item.setIdFood(resultSet.getInt("idFood"));
                item.setTitle(resultSet.getString("name"));
                item.setDescription(resultSet.getString("price"));
                item.setNote(resultSet.getString("note"));
                list.add(item);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            closeAll();
        }
        return list;
    }

    public List<HistoryBean> getHistoryBeanList(String phone) {
        List<HistoryBean> list = new ArrayList<>();
        try {
            getConnection();
            String sql = "select dish.name, torder.time, torder.amount " +
                    "from torder, dish " +
                    "where torder.idcustomer = ? and torder.idfood = dish.idfood;";
            callableStatement = connection.prepareCall(sql);
            callableStatement.setString(1, phone);
            resultSet = callableStatement.executeQuery();
            while (resultSet.next()) {
                HistoryBean item = new HistoryBean();
                item.setFoodName(resultSet.getString("dish.name"));
                item.setOrderTime(resultSet.getString("torder.time"));
                item.setOrderAmount(resultSet.getInt("torder.amount"));
                list.add(item);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            closeAll();
        }
        return list;
    }

    public Dish getDishById(int id) {
        Dish item = null;
        try {
            getConnection();
            String sql = "select * from Dish where idFood=?";
            callableStatement = connection.prepareCall(sql);
            callableStatement.setInt(1, id);
            resultSet = callableStatement.executeQuery();
            if (resultSet.next()) {
                item = new Dish();
                item.setIdFood(resultSet.getInt("idFood"));
                item.setName(resultSet.getString("name"));
                item.setPrice(resultSet.getInt("price"));
                item.setNote(resultSet.getString("note"));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            closeAll();
        }
        return item;
    }
}
